home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / ab20 / ab20_archive / utilities / emulators / unixlib.lzh / names.c < prev    next >
C/C++ Source or Header  |  1991-10-04  |  3KB  |  112 lines

  1. #include <sys/types.h>
  2. #include <sys/socket.h>
  3. #include <netdb.h>
  4. #include <netinet/in.h>
  5. #include <stdio.h>
  6.  
  7. extern char *malloc();
  8.  
  9. /* zoek een host met de gegeven naam in de file /etc/hosts */
  10. struct hostent *gethostbyname(host_name)
  11.     char *host_name;
  12. {
  13.     struct hostent *host;
  14.     char **aliases, **addr_list;
  15.  
  16.     char tmp[100];    /* should be long enough */
  17.     int a, b, c, d, count;
  18.     FILE *f;
  19.     char address[20]; /* 16+4 should be long enough */
  20.     f = fopen(":etc/hosts","r");
  21.     if(!f) {
  22.     fprintf(stderr, "Could not open :etc/hosts\n");
  23.     return NULL;
  24.     }
  25.     while((count = fscanf(f, "%d.%d.%d.%d %s", &a, &b, &c, &d, tmp)) == 5)
  26.     if(!strcmp(host_name, tmp))
  27.         break;
  28.      if(count != 5) {
  29.     fclose(f);
  30.     return NULL;
  31.     }
  32.     fclose(f);
  33.     /* ok data was read, now fill the hostent structure with it */
  34.     host = (struct hostent *) malloc(sizeof(struct hostent));
  35.     if(!host)
  36.         return NULL;
  37.     host->h_name = host_name;
  38.     aliases = (char **) malloc(4);    /* 1 pointer naar string */
  39.     if(!aliases)
  40.     return NULL;
  41.     aliases[0] = '\0';    /* geen aliases */
  42.     host->h_aliases = aliases;
  43.     addr_list = (char **) malloc(4);    /* 1 pointer naar string */
  44.     if(!addr_list)
  45.     return NULL;
  46.     sprintf(address, "%d.%d.%d.%d", a, b, c, d);
  47.     addr_list[0] = malloc(strlen(address)+1);
  48.     strcpy(addr_list[0], address);
  49.     host->h_addr_list = addr_list;
  50.     host->h_addrtype = AF_INET;
  51.     host->h_length = strlen(address);    /* lengte van address */
  52.     return host;
  53. }
  54.  
  55.  
  56. int gethostname(name, namelen)
  57.     char *name;
  58.     int namelen;
  59. {
  60.     FILE *f;
  61.     char tmp[100];    /* should be long enough */
  62.     int a,b,c,d;
  63.  
  64.     f = fopen(":etc/hosts","r");
  65.     if(!f) {
  66.     fprintf(stderr, "Could not open :etc/hosts\n");
  67.     return -1;
  68.     }
  69.     if(fscanf(f, "%d.%d.%d.%d %s", &a, &b, &c, &d, tmp) != 5) {
  70.     fprintf(stderr, "illegal line found in :etc/hosts");
  71.     fclose(f);
  72.     return -1;
  73.     }
  74.     if(strlen(tmp) > namelen) {
  75.     fclose(f);
  76.     return -1;
  77.     }
  78.     strcpy(name, tmp);
  79.     fclose(f);
  80.     return 0;
  81. }
  82.  
  83. int getpeername(s, name, namelen)
  84.     int s;
  85.     struct sockaddr *name;
  86.     int *namelen;
  87. {   /* put the name of the host to which the socket is connected in name */
  88.     char *this_host = "6.6.6.6";
  89.  
  90.     *namelen = strlen(this_host);
  91.     strcpy(name->sa_data, this_host);
  92.  
  93.     return 0;    /* success */
  94. }
  95.  
  96. char *inet_ntoa(in)
  97.     struct in_addr in;
  98. {
  99.     static char *address = "6.66.66.6";
  100.     return address;
  101. }
  102.  
  103. /*
  104.    6 = 0x06
  105.   66 = 0x42
  106. */
  107. unsigned long inet_addr(cp)
  108.     char *cp;
  109. {
  110.     return 0x06424206;
  111. }
  112.